package com.chx.three.mama.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chx.three.mama.R;
import com.chx.three.mama.model.FeelingDiary;
import com.chx.three.mama.utils.BitmapHelper;
import com.chx.three.mama.utils.ImageLoadingHandle;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import java.util.List;

/**
 * Created by Hainuo on 2015/3/25.
 */
public class FeelingDiaryAdapter extends BaseAdapter {
    private List<FeelingDiary> data;
    private LayoutInflater inflater;
    private View.OnClickListener deleteListener;

    public FeelingDiaryAdapter(Context context, List<FeelingDiary> data) {
        if (context != null) {
            inflater = LayoutInflater.from(context);
        } else {
            throw new IllegalArgumentException("Context must not null");
        }
        this.data = data;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (data != null) {
            ret = data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (data != null) {
            ret = data.get(position);
        }
        return ret;
    }

    public void setDeleteListener(View.OnClickListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        ret = inflater.inflate(R.layout.item_feeling_diary, parent, false);

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.month = (TextView) ret.findViewById(R.id.month);
            holder.day = (TextView) ret.findViewById(R.id.day);
            holder.content = (TextView) ret.findViewById(R.id.content);
            holder.babyBirth = (TextView) ret.findViewById(R.id.baby_birth);
            holder.time = (TextView) ret.findViewById(R.id.time);
            holder.dayIcon = (ImageView) ret.findViewById(R.id.icon_day);
            holder.diaryImg = (ImageView) ret.findViewById(R.id.diary_img);
            holder.deleteImg = (ImageView) ret.findViewById(R.id.delete_diary);

            ret.setTag(holder);
        }

        FeelingDiary diary = data.get(position);
        String month = diary.getMonth();
        String isfirst = diary.getIsFirst();

        month = month + "月";
        holder.month.setText(month);
        holder.time.setText(diary.getTime());
        holder.content.setText(diary.getContent());
        holder.babyBirth.setText(diary.getBabyBirth());
        holder.day.setText(diary.getDay());

        String url = diary.getImgUrl();
        if(url.equals("")) {
            holder.diaryImg.setVisibility(View.GONE);
        }else {
            holder.diaryImg.setVisibility(View.VISIBLE);
            BitmapHelper.getBitmapUtils().display(
                    holder.diaryImg,
                    url
            );
        }

        if(diary.isShow()){
            holder.dayIcon.setVisibility(View.INVISIBLE);
            holder.day.setVisibility(View.INVISIBLE);
            holder.month.setVisibility(View.INVISIBLE);
            holder.deleteImg.setVisibility(View.VISIBLE);
            if(deleteListener != null) {
                holder.deleteImg.setOnClickListener(deleteListener);
            }
        }else {
            holder.deleteImg.setVisibility(View.INVISIBLE);
            if (isfirst.equals("N")) {
                holder.dayIcon.setVisibility(View.INVISIBLE);
                holder.day.setVisibility(View.INVISIBLE);
                holder.month.setVisibility(View.INVISIBLE);
            }else {

                holder.dayIcon.setVisibility(View.VISIBLE);
                holder.day.setVisibility(View.VISIBLE);
                holder.month.setVisibility(View.VISIBLE);
            }
        }

        holder.deleteImg.setTag(position);

        return ret;
    }

    private static class ViewHolder {
        private TextView month;
        private TextView day;
        private TextView content;
        private TextView babyBirth;
        private TextView time;
        private ImageView dayIcon;
        private ImageView diaryImg;
        private ImageView deleteImg;
    }
}
